//
//  LandmarkDetail.swift
//  demo
//
//  Created by raymond on 2022/5/10.
//

import SwiftUI
import MapKit

struct LandmarkDetail: View {
  @EnvironmentObject var modelData: ModelData
  var landmark: Landmark
  
  var landmarkIndex: Int {
    modelData.landmarks.firstIndex(where: {$0.id==landmark.id})!
  }
  
  var body: some View {
    ScrollView {
      MapView(coordinate: CLLocationCoordinate2D(latitude: landmark.locationCoordinate.latitude, longitude: landmark.locationCoordinate.longitude))
      .ignoresSafeArea(edges: .top)
      .frame(height: 300)

      NatureImage(image:landmark.image)
      .offset(y: -130)
      .padding(.bottom, -130)

      VStack(alignment: .leading) {
        HStack {
          Text(landmark.name).font(.title)
          FavoriteButton(isSet: $modelData.landmarks[landmarkIndex].isFavorite)
        }

        HStack {
          Text(landmark.park)
          Spacer()
          Text(landmark.state)
        }
        .font(.subheadline)
        .foregroundColor(.secondary)

        Divider()

        Text("About \(landmark.name)")
          .font(.title2)
        Text(landmark.description)
      }
      .padding()
    }
    .navigationTitle(landmark.name)
    .navigationBarTitleDisplayMode(.inline)
  }
}

struct LandmarkDetail_Previews: PreviewProvider {
  static let modelData = ModelData()
  static var previews: some View {
    LandmarkDetail(landmark: modelData.landmarks[0]).environmentObject(modelData)
  }
}
